class EventBus {
  private events: Record<string, Function[]> = {};

  on(eventName: string, callback: Function) {
    if (!this.events[eventName]) {
      this.events[eventName] = [];
    }
    this.events[eventName].push(callback);
  }

  emit(eventName: string, data?: any) {
    const callbacks = this.events[eventName];
    if (callbacks) {
      callbacks.forEach((cb) => cb(data));
    }
  }

  off(eventName: string, callback: Function) {
    const index = this.events[eventName]?.indexOf(callback);
    if (index !== -1) {
      this.events[eventName].splice(index!, 1);
    }
  }

  once(eventName: string, callback: Function) {
    const wrapper = (data: any) => {
      callback(data);
      this.off(eventName, wrapper);
    };
    this.on(eventName, wrapper);
  }
}

export default new EventBus();
